{include file="header/index" /}
<div class="newContentBox">
  <div class="fixedFather">
    <div class="fix">
      添加管理员
      <span class="newNotice"><span class="newtext">*</span>为必填项</span>
      <div class="newFixBtnBox">
        <button type="button" class="newBtnDefault newBtnGray" onclick="returnBack(this)"> <i
            class="fa fa-mail-reply"></i>返回</button>
      </div>
    </div>
  </div>
  <form action="" class="layui-form newListPart ">
    <div class="newPart">
      <div class="newTitle">
        <i class="fa fa-address-book allColor icon"></i> 基本信息
      </div>
      <div class="newContent">
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span> 管理员账号</label>
          <div class="layui-input-block cover_layui_i_b">
            <input id="account" type="text" name="account" placeholder="输入管理员帐号" required lay-verify="required"
              autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span> 管理员用户组</label>
          <div class="layui-input-block cover_layui_i_b">
            <select name="groupid" id="groupid">
              <option value="">==请选择用户组==</option>
              　{foreach $data as $v}
              <option value="{$v.id}">{$v.title}</option>
              　{/foreach}
            </select>
          </div>
        </div>
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span>头像</label>
          <div class="layui-input-block cover_layui_i_b layui-upload">
            <button type="button" class="layui-btn newBtnPrimary" id="uploadOne">选择头像</button>
            <div class="layui-upload-list">
              <div class="uploadImg manegImg">
                <img src="__IMG__/head_default.gif" height="80px" width="80px" alt="">
                <div class="remove" onclick="removeImg(this)"><i class="fa fa-times"></i></div>
              </div>
            </div>
          </div>
        </div>
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span> 登录密码</label>
          <div class="layui-input-block cover_layui_i_b">
            <input id="password" type="text" name="password" placeholder="输入登录密码" required lay-verify="required"
              autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span> 真实姓名</label>
          <div class="layui-input-block cover_layui_i_b">
            <input id="name" type="text" name="name" placeholder="输入管理员真实姓名" required lay-verify="required"
              autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span>手机号码</label>
          <div class="layui-input-block cover_layui_i_b">
            <input id="phone" type="text" name="phone" placeholder="输入管理员手机号" required lay-verify="required"
              autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span> 邮箱</label>
          <div class="layui-input-block cover_layui_i_b">
            <input id="email" type="text" name="email" placeholder="输入管理员邮箱" required lay-verify="required"
              autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span> 联系地址</label>
          <div class="layui-input-block cover_layui_i_b">
            <input id="address" type="text" name="address" placeholder="输入管理员联系地址" required lay-verify="required"
              autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item cover_layui_f_i">
          <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span>状态</label>
          <div class="layui-input-block cover_layui_i_b">
            <input type="checkbox" name="status" value="1" lay-skin="primary" title="" checked="true">
            <span style="position: absolute;top: 10px;">默认开启</span>
          </div>
        </div>
      </div>
      <div class="layui-form-item ">
        <div class="layui-input-block cover_layui_i_b">
          <button class="layui-btn newBtnDefault newBtnPrimary" lay-submit lay-filter="formDemo">保存</button>
          <button type="reset" class="layui-btn newBtnDefault">重置</button>
        </div>
      </div>
    </div>
  </form>
</div>
<script>
  var layer = layui.layer,
    element = layui.element,
    laydate = layui.laydate,
    laypage = layui.laypage,
    form = layui.form,
    upload = layui.upload,
    layedit = layui.layedit;
  form.render();
</script>
<script type="text/javascript">
  //重置
  function reset() {
    top.layer.confirm('你确定要重置吗?', {
      btn: ['确定', '取消'] //按钮
    }, function () {
      $('#userAdd')[0].reset();
      top.layer.msg('重置成功', {
        icon: 1
      });
    });
  }
  //提交表单
  var flag = false;
  form.on('submit(formDemo)', function (data) {
    if (flag) {
      return false;
    }
    flag = true;
    delete data.field.file //true
    $.post("{:url('Users/addUser')}", data.field, function (e) {
      if (e.code == 1) {
        top.layer.msg(e.msg, {
          icon: 1,
          time: 2000
        }, function () {
          top.layer.close();
          window.location.href = e.data;
        });
      } else {
        top.layer.msg(e.msg, {
          icon: 2,
          time: 2000
        });
      }
      flag = false;
    }, 'json');
    return false;
  });
  // 单图头像
  var uploadOne = upload.render({
    elem: '#uploadOne',
    url: '{:url("commonapi/upload/uploadImg")}',
    done: function (res) {
      //如果上传失败
      if (res.code == -1000) {
        return layer.msg('上传失败');
      } else {
        var html = '<div class="uploadImg manegImg">' +
          '<input type="hidden" value="' + res.data.url + '" name="avatar_img">' +
          '<img src="' + res.data.all_url + '" height="80px" width="80px" alt="">' +
          '<div class="remove" onclick="removeImg(this)"><i class="fa fa-times"></i></div>' +
          '</div>'
        $("#uploadOne").siblings('.layui-upload-list').html(html);
      }
      //上传成功
    },
    error: function () {
      layer.msg('上传失败', {
        icon: 2
      })
    }
  });

  /**
   * ajax 表单提交
   */
  function addUser() {
    if ($.trim($("input[name='account']").val()) == '') {
      layer.msg('请填写管理员帐号', {
        icon: 2,
        time: 2000
      });
      return false;
    }

    if ($.trim($("input[name='password']").val()) == '') {
      layer.msg('请填写登录密码', {
        icon: 2,
        time: 2000
      });
      return false;
    }

    if ($.trim($("input[name='name']").val()) == '') {
      layer.msg('请填写管理员真实姓名', {
        icon: 2,
        time: 2000
      });
      return false;
    }

    if ($.trim($("input[name='phone']").val()) == '') {
      layer.msg('请填写管理员手机号', {
        icon: 2,
        time: 2000
      });
      return false;
    }

    if ($.trim($("input[name='email']").val()) == '') {
      layer.msg('请填写管理员邮箱', {
        icon: 2,
        time: 2000
      });
      return false;
    }


    if ($.trim($("input[name='email']").val()) == '') {
      layer.msg('请填写管理员邮箱', {
        icon: 2,
        time: 2000
      });
      return false;
    }


    if ($.trim($("#groupid").find('option:selected').val()) == '') {
      layer.msg('请选择用户组', {
        icon: 2,
        time: 2000
      });
      return false;
    }


    if (!$("#email").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) {
      layer.msg('邮箱格式不正确', {
        icon: 2,
        time: 2000
      });
      return false;
    }

    if (!(/^1[34578]\d{9}$/.test($.trim($("input[name='phone']").val())))) {
      layer.msg('手机号码格式不正确', {
        icon: 2,
        time: 2000
      });
      return false;
    }

    if ($.trim($("input[name='address']").val()) == '') {
      layer.msg('请填写管理员地址', {
        icon: 2,
        time: 2000
      });
      return false;
    }


    var formData = new FormData($("#userAdd")[0]);
    $.ajax({
      url: "{:url('Users/addUser')}",
      /*这是处理文件上传的servlet*/
      type: 'POST',
      data: formData,
      async: false,
      cache: false,
      contentType: false,
      processData: false,
      dataType: 'json',
      success: function (returndata) {
        if (returndata.code == '1') {
          layer.msg(returndata.msg, {
            icon: 1,
            time: 2000
          }, function () {
            window.location.href = returndata.data
          });
        } else {
          layer.msg(returndata.msg, {
            icon: 2,
            time: 2000
          });
        }

      },
      error: function (returndata) {
        layer.msg('数据异常', {
          icon: 2,
          time: 2000
        });
        return false;
      }
    });
  }


  /**
   * 图片显示
   */
  // function qwe() {
  // 	var file = document.getElementById("avatar_img").files[0];
  // 	var reader = new FileReader();
  // 	reader.onload = function(e) {
  // 		img_data.src = this.result;
  // 	};
  // 	reader.readAsDataURL(file); //Base64
  //   }
</script>